const { read, write } = require('../lib/read&write')

const HOTSEARCHURL = '../data/hotSearches.json'

function getAllHotSearches() {
  const __hotSearches = read(HOTSEARCHURL)
  return __hotSearches
}

function addHotSearch(hotSearch) {
  const __hotSearches = read(HOTSEARCHURL)
  __hotSearches.push(hotSearch)
  write(__hotSearches, HOTSEARCHURL)
}

function removeHotSearch(hotSearch) {
  const __hotSearches = read(HOTSEARCHURL)
  __hotSearches.splice(
    __hotSearches.findIndex((item) => item === hotSearch),
    1
  )
  write(__hotSearches, HOTSEARCHURL)
}

function editHotSearch(hotSearch) {
  const __hotSearches = read(HOTSEARCHURL)
  __hotSearches.splice(
    __hotSearches.findIndex((item) => item.content === hotSearch.content),
    1,
    hotSearch
  )
  write(__hotSearches, HOTSEARCHURL)
}

function searchHotSearch(hotSearch) {
  const __hotSearches = read(HOTSEARCHURL)
  const hasArr = __hotSearches.find((item) => item.content === hotSearch)
  return hasArr
}



module.exports = {
  getAllHotSearches,
  addHotSearch,
  removeHotSearch,
  searchHotSearch, editHotSearch
}